home *** CD-ROM | disk | FTP | other *** search
/ FishMarket 1.0 / FishMarket v1.0.iso / fishies / 076-100 / disk_085 / csh / run.c < prev    next >
C/C++ Source or Header  |  1992-05-06  |  3KB  |  122 lines

  1.  
  2. /*
  3.  * RUN.C
  4.  *
  5.  * (c)1986 Matthew Dillon     9 October 1986
  6.  *
  7.  *    RUN   handles running of external commands.
  8.  *
  9.  * version 2.06M (Manx Version and Additions) by Steve Drew 28-May-87
  10.  *
  11.  */
  12.  
  13. #include "shell.h"
  14.  
  15. char *FindIt();
  16.  
  17. do_run(str)
  18. char *str;
  19. {
  20.    int i, try = -1;
  21.    int run = 0;
  22.    char buf[128];
  23.    char runcmd[128];
  24.    char *save, *path, *index();
  25.  
  26.    char *p = av[0];
  27.    char **args = av+1;
  28.    
  29.    while(*p++) *p &= 0x7F;    /* allow "com mand" */
  30.  
  31.    while(*args) {                  /* if any arg contains a space then */
  32.       if (index(*args,' ')) {      /* surround with quotes, since must */
  33.          i = strlen(*args);        /* of specified via "arg u ment" on */
  34.      movmem(*args,(*args)+1,i);/* original command line.           */
  35.          args[0][0] = args[0][i+1] = '\"';  /* mpush in execom.c has   */
  36.          args[0][i+2] = '\0';      /* allowed for these 2 extra bytes. */
  37.       }
  38.       ++args;
  39.    }
  40.    
  41.    if (path = FindIt(av[0],"",buf)) {
  42.       if (!strcmp(av[0],"run") || !strcmp(av[0],"ru")) {      /* was a run */
  43.      if (FindIt(av[1],"",runcmd)) {
  44.         run = 1;
  45.         save = av[1];
  46.         av[1] = runcmd;
  47.      }
  48.       }
  49.       if ((try = fexecv(path, av)) == 0)
  50.      i = wait();
  51.       if (run) av[1] = save;
  52.    }
  53.    else {
  54.       APTR original;
  55.       original = Myprocess->pr_WindowPtr;
  56.       Myprocess->pr_WindowPtr = (APTR)(-1);
  57.       if ((try = fexecv(av[0], av)) == 0)
  58.      i = wait();
  59.       Myprocess->pr_WindowPtr = original;
  60.    }
  61.    if (try) {
  62.       long lock;
  63.       char *copy;
  64.  
  65.       if ((path = FindIt(av[0],".sh",buf)) == NULL) {
  66.      fprintf(stderr,"Command Not Found %s\n",av[0]);
  67.      return (-1);
  68.       }
  69.       av[1] = buf;         /* particular to do_source() */
  70.       copy = malloc(strlen(str)+3);
  71.       strcpy(copy+2,str);
  72.       copy[0] = 'x';
  73.       copy[1] = ' ';
  74.       i = do_source(copy);
  75.       free(copy);
  76.    }
  77.    return (i);
  78. }
  79.  
  80.  
  81. char *
  82. FindIt(cmd, ext, buf)
  83. char *cmd;
  84. char *ext;
  85. char *buf;
  86. {
  87.    long lock = 0;
  88.    char hasprefix = 0;
  89.    APTR original;
  90.    char *ptr, *s = NULL;
  91.  
  92.    original = Myprocess->pr_WindowPtr;
  93.  
  94.    for (ptr = cmd; *ptr; ++ptr) {
  95.       if (*ptr == '/' || *ptr == ':')
  96.      hasprefix = 1;
  97.    }
  98.  
  99.    if (!hasprefix) {
  100.     Myprocess->pr_WindowPtr = (APTR)(-1);
  101.     s = get_var(LEVEL_SET, V_PATH);
  102.    }
  103.  
  104.    strcpy(buf, cmd);
  105.    strcat(buf, ext);
  106.    while ((lock = (long)Lock(buf, ACCESS_READ)) == 0) {
  107.       if (*s == NULL || hasprefix) break;
  108.       for(ptr = s; *s && *s != ','; s++) ;
  109.       strcpy(buf, ptr);
  110.       buf[s-ptr] = '\0';
  111.       strcat(buf, cmd);
  112.       strcat(buf, ext);
  113.       if (*s) s++;
  114.    }
  115.    Myprocess->pr_WindowPtr = original;
  116.    if (lock) {
  117.       UnLock(lock);
  118.       return(buf);
  119.    }
  120.    return(NULL);
  121. }
  122.